package com.kgc.mj.springbooot.controller;

import com.kgc.mj.springbooot.service.AsyncChargeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by MJ on 2020/10/21 9:08
 */
@RestController
public class AsyncController {

    //日志
    private Logger logger = LoggerFactory.getLogger(AsyncController.class);

    @Autowired
    private AsyncChargeService asyncChargeService;

    @GetMapping(value = "/rcvCharge")
    public String rcvPhone(@RequestParam String phoneNo){
        long currentTime1 = System.currentTimeMillis();
        logger.info("========收到"+phoneNo+"的充值请求，生成充值订单成功，并调用充值接口=======");
        asyncChargeService.executeAsyncCharge(phoneNo);
        long currentTime2 = System.currentTimeMillis();
        logger.info("========总耗时:"+(currentTime2-currentTime1)+"ms=======");

        return phoneNo+"充值成功";
    }

    /*
    * 处理充值请求
    * @Async是异步
    * */
    public void  asyncCharge(String phoneNo) {
        logger.info("========" + phoneNo + ",开始充值=======");
        long currentTimeStart = System.currentTimeMillis();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            logger.error("充值异常。异常信息为" + e.getMessage());
        }
        long currentTimeEnd = System.currentTimeMillis();
        logger.info("========充值耗时" + (currentTimeEnd - currentTimeStart) + ",=======");
        logger.info("========" + phoneNo + "充值完成");
    }
}


